home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / modula2 / 148 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: fourier.newcastle.edu.au!peter
  2. From: peter@fourier.newcastle.edu.au (Peter Moylan)
  3. Newsgroups: comp.lang.modula2
  4. Subject: Re: Modula2 for C programmers?
  5. Date: 24 Jan 1996 00:03:58 GMT
  6. Organization: The University of Newcastle
  7. Message-ID: <4e3t1f$d5q@seagoon.newcastle.edu.au>
  8. References: <erico-1801961940460001@infinitehell.cnmat.berkeley.edu> <4dqusf$d7k@weck.brokersys.com> <DLIGLJ.FDE@nyongwa.montreal.qc.ca> <4du11j$l31@weck.brokersys.com>
  9. Reply-To: peter@tesla.newcastle.edu.au
  10. NNTP-Posting-Host: fourier.newcastle.edu.au
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. jguthrie@brokersys.com wrote:
  14. >Michel De Rosa (aliath@nyongwa.montreal.qc.ca) wrote:
  15.  
  16. >: Bit operations.. unfortunately with pre-ISO compilers, we where usually
  17. >: stuck with multiplying or dividing by 2, for shift operations (rotate is
  18. >: a tad more involved), now ISO also provides functions for those.. for
  19. >: and/or/xor etc.. it's always been possible to do these, using the set
  20. >: operators.. first coerce to a BITSET...
  21.  
  22. >Please don't talk down to me.  I know how to manipulate bitsets, but 
  23. >do WHAT to a BITSET?   As far as I know, NONE of the M2 compilers I have
  24. >ever used allow me to convert between any kind of number and any kind of
  25. >set.  Otherwise, I'd have used that rather than mucking about with assembly
  26. >language.  
  27.  
  28. >I know that that isn't mentioned anywhere in PIM3C.
  29.  
  30. Yes it is.  Let c be a CARDINAL (for example) and let b be a BITSET.
  31. Then in ISO M2 you can write  b := CAST(BITSET, c).  In the PIM3
  32. version of M2 the syntax is more obscure:  b := BITSET(c).
  33. The second form should be supported by any M2 compiler you can
  34. lay your hands on.
  35.  
  36. A trick used by some programmers is to abuse the concept of
  37. a record with variants:
  38.  
  39.      TYPE Cheat = RECORD
  40.                       CASE :BOOLEAN OF
  41.                           | FALSE: bits: BITSET;
  42.                           | TRUE:  val: CARDINAL;
  43.                       END (*CASE*)
  44.                   END (*RECORD*)
  45.  
  46. Now, with x a variable of type Cheat, you can do bit operations
  47. on x.bits, and the answer will turn up in x.val.  This isn't
  48. portable, but then operations requiring type-casting are
  49. inherently non-portable in any language.
  50.  
  51. --
  52. Peter Moylan                       peter@ee.newcastle.edu.au
  53.                            ftp://ee.newcastle.edu.au/pub/www/Moylan.html
  54.